/**
 *  每周五执行，检索下周的周计划，
 *  条件：所有人的简档=销售部分销销售员、销售部分销经理/主管、销售部直销销售员、销售部直销经理/主管，
 *  如果没有找到下周的周计划，新建任务提醒
 *  created by Harry 2017-01-10 20:14:21
 */
global class CheckWeeklyPlanSchedule implements Schedulable {
    global void execute(SchedulableContext sc) {
    	Date today = Date.today();
        //  下周一，因为需求写死周五执行，所以如果定时任务改为每周的其他天执行，下面的代码需要修改
        Date lastMonday = today.addDays(-7);
    	//Date nextMonday = today.addDays(3);
        //  下周日，同上
        //Date nextSunday = today.addDays(10);
        Set<String> profileSet = new Set<String>();
        // {
        //     '销售部分销销售员',
        //     '销售部分销经理/主管',
        //     '销售部直销经理/主管',
        //     '销售部直销销售员'};

        String profileStr = Label.ProfilesofWeeklyPlan;
        List<String> profileList = profileStr.split(',');
        if(null != profileList){
            profileSet.addAll(profileList);
        }

        // 缓存，用来存储[用户]和[周计划]的ID映射
        Map<Id,String > userWPMap = new Map<Id,String >();

        for(User u :[
            SELECT Id,Profile.Name ,WeekPlan__c 
            FROM User WHERE Profile.Name IN : profileSet AND IsActive = true]){
            userWPMap.put(u.Id , u.WeekPlan__c);
        }

      /* Map<Id,Weekly_Plan__c> weekPlans = new Map<Id,Weekly_Plan__c>([
            SELECT Id,Owner.Profile.Name
            FROM Weekly_Plan__c
            WHERE Start_Date__c >= : nextMonday AND Start_Date__c <= :nextSunday ]);
        */
            //上周的周计划
            Map<Id,String> doMap = new Map<Id,String>();
         for(Weekly_Plan__c w : [
            SELECT Id,OwnerId
            FROM Weekly_Plan__c
            WHERE Start_Date__c >= : lastMonday AND Start_Date__c < :today ]){

                doMap.put(w.OwnerId ,'是');
         }

         List<User> uList = new List<User>();
         for(Id uid: userWPMap.keySet()){
            
            if(doMap.containsKey(uid)){
                if(userWPMap.get(uid) == null || doMap.get(uid) != userWPMap.get(uid) ){

                    uList.add(new User(Id = uid ,WeekPlan__c ='是'));
                }
            }else{
                if(userWPMap.get(uid)  == null || userWPMap.get(uid) != '否' )uList.add(new User(Id = uid ,WeekPlan__c ='否'));
            }
        }

        if(uList.size() > 0 ) update uList;
        
        //  将所有符合条件的[用户]ID存入缓存，其对应的[周计划]ID初始化为null
        /*for(Id id: users.keySet()){
            userWPMap.put(id,null);
        }*/


        //  将有填写了[周计划]的记录ID存入缓存
        /*for(Id id : weekPlans.keySet()){
            Weekly_Plan__c WP = weekPlans.get(id);
            userWPMap.put(WP.OwnerId,id);
        }*/




        //  缓存中[月计划]ID仍为null的既是没有填写[月计划]的用户，需要新建任务提醒
        /*List<Task> taskList = new List<Task>();
        for(Id userId : userWPMap.keySet()){
            Id WPId = userWPMap.get(userId);
            if(null == WPId){
                Task t = new Task();
                t.ActivityDate = Date.today();
                t.OwnerId = userId;
                t.Subject = '请及时填写下周的周计划';
                taskList.add(t);
            }
        }


        insert taskList;*/
    }
}